#!/usr/bin/env python3

import numpy as np
import matplotlib.pyplot as plt

import pywt

def image_process():
    from PIL import Image
    original = np.asarray(Image.open('../src/lenna.jpg'), dtype=float)
    original = original[:,:,0]

    # def convert(x):
    #     np.putmask(x, x<0, 0)
    #     np.putmask(x, x>255, 255)

    # Wavelet transform of image, and plot approximation and details
    titles = ['Contour', ' Horizontal detail',
              'Vertical detail', 'Diagonal detail']
    coeffs2 = pywt.dwt2(original, 'db2')
    LL, (LH, HL, HH) = coeffs2
    fig = plt.figure(figsize=(6, 5))
    axes = fig.subplots(2, 2)
    for i, a in enumerate([LL, LH, HL, HH]):
        ax = axes[divmod(i,2)]
        ax.imshow(a, interpolation="nearest", cmap=plt.cm.gray)
        ax.set_title(titles[i], fontsize=10)
        ax.set_xticks([])
        ax.set_yticks([])

    fig.tight_layout()
    plt.show()


import h5py

def signal_proces():
    

    with h5py.File("timing_samples.h5", 'r') as file_in:
        hit = file_in["hit"][()]
        nonhit = file_in["nonhit"][()]

    x=hit["PulseTime"]

    fig = plt.figure()
    axes = fig.subplots(3, 1)
    original, b, _= ax.hist(x, bins=1024)

    coeffs = pywt.dwt(original, 'db2')
    L, H = coeffs

    # def op(d):
    #     s = np.sort(np.abs(d))
    #     L = len(s)
    #     t = s.tolist()[L//64*63]
    #     return d.truncate(t)

    axes[0].stem(original)
    axes[1].plot(L)
    axes[2].plot(H)
    plt.show()

signal_proces()